<?php

/**
 * 订单管理
 *
 * @author birdy <freebirdy@foxmail.com>
 * @link http://www.aiyuangong.com
 */

namespace app\seller\controller;

class Order extends \app\common\controller\Seller
{

    public function index()
    {
        $model_order = model('Common/Order');
        $condition = array();
        $condition['store_id'] = $this->store_id;
        $order_sn = input('order_sn');
        $state_type = input('state_type');
        $buyer_name = input('buyer_name');
        if ($order_sn) {
            $condition['order_sn'] = $order_sn;
        }
        if ($buyer_name) {
            $condition['buyer_name'] = $buyer_name;
        }
        $allow_state_array = array('state_new', 'state_pay', 'state_send', 'state_success', 'state_cancel');
        if (in_array($state_type, $allow_state_array)) {
            $condition['order_state'] = str_replace($allow_state_array, array(ORDER_STATE_NEW, ORDER_STATE_PAY, ORDER_STATE_SEND, ORDER_STATE_SUCCESS, ORDER_STATE_CANCEL), $state_type);
        } else {
            $state_type = 'store_order';
        }
        
        $query_date = $this->query_date();
        if ($query_date && is_array($query_date)) {
            $condition['add_time'] = $query_date;
        }

        if (input('skip_off') == 1) {
            $condition['order_state'] = array('neq', ORDER_STATE_CANCEL);
        }

        $pageConfig = [
            'path' => url('seller/order/index'),
            'query' => $this->request->param(),
            'page' => input('curpage/d') ? : 1,
            'type' => '\\Page',
            'var_page' => 'curpage'
        ];
        
        $order_list = $model_order->getOrderList($condition, 10, '*', 'order_id desc', $pageConfig, array('order_goods', 'order_common', 'member'));
        if (isset($order_list['page'])) {
            $this->assign('show_page', $order_list['page']);
        }
        $order_list = $order_list['list'] ? : [];

        //页面中显示那些操作
        foreach ($order_list as $key => $order_info) {

            //显示取消订单
            $order_info['if_cancel'] = $model_order->getOrderOperateState('store_cancel', $order_info);

            //显示调整运费
            $order_info['if_modify_price'] = $model_order->getOrderOperateState('modify_price', $order_info);

            //显示修改价格
            $order_info['if_spay_price'] = $model_order->getOrderOperateState('spay_price', $order_info);

            //显示发货
            $order_info['if_send'] = $model_order->getOrderOperateState('send', $order_info);

            //显示锁定中
            $order_info['if_lock'] = $model_order->getOrderOperateState('lock', $order_info);

            //显示物流跟踪
            $order_info['if_deliver'] = $model_order->getOrderOperateState('deliver', $order_info);

            foreach ($order_info['extend_order_goods'] as $value) {
                $value['image_60_url'] = img_url($value['goods_image'], 60);
                $value['image_240_url'] = img_url($value['goods_image'], 240);
                $value['goods_type_cn'] = orderGoodsType($value['goods_type']);
                $value['goods_url'] = url('index/goods/detail', array('goods_id' => $value['goods_id']));
                if ($value['goods_type'] == 5) {
                    $order_info['zengpin_list'][] = $value;
                } else {
                    $order_info['goods_list'][] = $value;
                }
            }

            if (empty($order_info['zengpin_list'])) {
                $order_info['goods_count'] = count($order_info['goods_list']);
            } else {
                $order_info['goods_count'] = count($order_info['goods_list']) + 1;
            }
            $order_list[$key] = $order_info;
        }

        $this->assign('order_list', $order_list);
        //$this->assign('show_page', $model_order->showpage());
        $this->profile_menu('list', $state_type);

        $this->assign('left_nav', 'order');
        return view();
    }

    /**
     * 卖家订单详情
     *
     */
    public function detail()
    {
        $order_id = input('order_id/d');
        if ($order_id <= 0) {
            return $this->error('参数不正确');
        }
        $model_order = model('Common/Order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['store_id'] = $this->store_id;
        $order_info = $model_order->getOrderInfo($condition, array('order_common', 'order_goods', 'member'));
        $order_pay = $model_order->getOrderPayInfo(array('pay_sn' => $order_info['pay_sn']));
        if (empty($order_info)) {
            return $this->error('该订单不存在');
        }

        $model_refund_return = model('Common/RefundReturn');
        $order_list = array();
        $order_list[$order_id] = $order_info;
        $order_list = $model_refund_return->getGoodsRefundList($order_list, 1); //订单商品的退款退货显示
        $order_info = $order_list[$order_id];
        $refund_all = $order_info['refund_list'][0];
        if (!empty($refund_all) && $refund_all['seller_state'] < 3) {//订单全部退款商家审核状态:1为待审核,2为同意,3为不同意
            $this->assign('refund_all', $refund_all);
        }

        //显示锁定中
        $order_info['if_lock'] = $model_order->getOrderOperateState('lock', $order_info);

        //显示调整运费
        $order_info['if_modify_price'] = $model_order->getOrderOperateState('modify_price', $order_info);

        //显示调整价格
        $order_info['if_spay_price'] = $model_order->getOrderOperateState('spay_price', $order_info);

        //显示取消订单
        $order_info['if_cancel'] = $model_order->getOrderOperateState('buyer_cancel', $order_info);

        //显示发货
        $order_info['if_send'] = $model_order->getOrderOperateState('send', $order_info);

        //显示物流跟踪
        $order_info['if_deliver'] = $model_order->getOrderOperateState('deliver', $order_info);

        //显示系统自动取消订单日期
        if ($order_info['order_state'] == ORDER_STATE_NEW) {
            //$order_info['order_cancel_day'] = $order_info['add_time'] + ORDER_AUTO_CANCEL_DAY * 24 * 3600;

            $order_info['order_cancel_day'] = $order_info['add_time'] + ORDER_AUTO_CANCEL_DAY + 3 * 24 * 3600;
        }

        //显示快递信息
        if ($order_info['shipping_code'] != '') {
            $express = \Express::lists(true, 'id');
            $order_info['express_info']['e_code'] = $express[$order_info['extend_order_common']['shipping_express_id']]['e_code'];
            $order_info['express_info']['e_name'] = $express[$order_info['extend_order_common']['shipping_express_id']]['e_name'];
            $order_info['express_info']['e_url'] = $express[$order_info['extend_order_common']['shipping_express_id']]['e_url'];
        }

        //显示系统自动收获时间
        if ($order_info['order_state'] == ORDER_STATE_SEND) {
            //$order_info['order_confirm_day'] = $order_info['delay_time'] + ORDER_AUTO_RECEIVE_DAY * 24 * 3600;
            //
			$order_info['order_confirm_day'] = $order_info['delay_time'] + ORDER_AUTO_RECEIVE_DAY + 7 * 24 * 3600;
        }

        //如果订单已取消，取得取消原因、时间，操作人
        if ($order_info['order_state'] == ORDER_STATE_CANCEL) {
            $order_info['close_info'] = $model_order->getOrderLogInfo(array('order_id' => $order_info['order_id']), 'log_id desc');
        }

        foreach ($order_info['extend_order_goods'] as $value) {
            $value['image_60_url'] = img_url($value['goods_image'], 60);
            $value['image_240_url'] = img_url($value['goods_image'], 240);
            $value['goods_type_cn'] = orderGoodsType($value['goods_type']);
            $value['goods_url'] = url('index/goods/detail', array('goods_id' => $value['goods_id']));
            if ($value['goods_type'] == 5) {
                $order_info['zengpin_list'][] = $value;
            } else {
                $order_info['goods_list'][] = $value;
            }
        }

        if (empty($order_info['zengpin_list'])) {
            $order_info['goods_count'] = count($order_info['goods_list']);
        } else {
            $order_info['goods_count'] = count($order_info['goods_list']) + 1;
        }

        $this->assign('order_info', $order_info);
        $this->assign('order_pay', $order_pay);
        //发货信息
        if (!empty($order_info['extend_order_common']['daddress_id'])) {
            $daddress_info = model('Common/Daddress')->getAddressInfo(array('address_id' => $order_info['extend_order_common']['daddress_id']));
            $this->assign('daddress_info', $daddress_info);
        }

        $this->assign('left_nav', 'order');
        return view('detail');
    }

    /**
     * 卖家订单状态操作
     */
    public function change_state()
    {
        $state_type = input('state_type');
        $order_id = input('order_id/d');

        $model_order = model('Common/Order');
        $condition = array();
        $condition['order_id'] = $order_id;
        $condition['store_id'] = $this->store_id;
        $order_info = $model_order->getOrderInfo($condition);

        if ($state_type == 'order_cancel') {
            $result = $this->_order_cancel($order_info, input('post.'));
        } elseif ($state_type == 'modify_price') {
            $result = $this->_order_ship_price($order_info, input('post.'));
        } elseif ($state_type == 'spay_price') {
            $result = $this->_order_spay_price($order_info, input('post.'));
        }
        
        if (is_string($result)) {
            return $result;
        }
        
        if (!$result['status']) {
            return showDialog($result['msg'], '', 'error', $this->request->isAjax() ? '' : 'CUR_DIALOG.close();');
        } else {
            return showDialog($result['msg'], 'reload', 'succ', $this->request->isAjax() ? '' : 'CUR_DIALOG.close();');
        }
    }

    /**
     * 取消订单
     * @param unknown $order_info
     */
    private function _order_cancel($order_info, $post)
    {
        $model_order = model('Common/Order');
        $logic_order = model('Common/Order', "logic");

        if (!$this->request->isPost()) {
            $this->assign('order_info', $order_info);
            $this->assign('order_id', $order_info['order_id']);
            return $this->fetch('cancel');
        } else {
            $if_allow = $model_order->getOrderOperateState('store_cancel', $order_info);
            if (!$if_allow) {
                return callback(false, '无权操作');
            }
            $msg = $post['state_info1'] != '' ? $post['state_info1'] : $post['state_info'];
            return $logic_order->changeOrderStateCancel($order_info, 'seller', $this->member_info['name'], $msg);
        }
    }

    /**
     * 修改运费
     * @param unknown $order_info
     */
    private function _order_ship_price($order_info, $post)
    {
        $model_order = model('Common/Order');
        $logic_order = model('Common/Order', "logic");
        if (!$this->request->isPost()) {
            $this->assign('order_info', $order_info);
            $this->assign('order_id', $order_info['order_id']);
            return $this->fetch('edit_price');
        } else {
            $if_allow = $model_order->getOrderOperateState('modify_price', $order_info);
            if (!$if_allow) {
                return callback(false, '无权操作');
            }
            return $logic_order->changeOrderShipPrice($order_info, 'seller', $this->member_info['name'], $post['shipping_fee']);
        }
    }

    /**
     * 修改商品价格
     * @param unknown $order_info
     */
    private function _order_spay_price($order_info, $post)
    {
        $model_order = model('Common/Order');
        $logic_order = model('Common/Order', "logic");
        if (!$this->request->isPost()) {
            $this->assign('order_info', $order_info);
            $this->assign('order_id', $order_info['order_id']);
            return $this->fetch('edit_spay_price');
        } else {
            $if_allow = $model_order->getOrderOperateState('spay_price', $order_info);
            if (!$if_allow) {
                return callback(false, '无权操作');
            }
            return $logic_order->changeOrderSpayPrice($order_info, 'seller', $this->member_info['name'], $post['goods_amount']);
        }
    }

    /**
     * 用户中心右边，小导航
     *
     * @param string	$menu_type	导航类型
     * @param string 	$menu_key	当前导航的menu_key
     * @return
     */
    private function profile_menu($menu_type = '', $menu_key = '')
    {
        switch ($menu_type) {
            case 'list':
                $menu_array = array(
                    array('menu_key' => 'store_order', 'menu_name' => '所有订单', 'menu_url' => url('seller/order/index')),
                    array('menu_key' => 'state_new', 'menu_name' => '待付款', 'menu_url' => url('seller/order/index?state_type=state_new')),
                    array('menu_key' => 'state_pay', 'menu_name' => '待发货', 'menu_url' => url('seller/order/index?state_type=state_pay')),
                    array('menu_key' => 'state_send', 'menu_name' => '已发货', 'menu_url' => url('seller/order/index?state_type=state_send')),
                    array('menu_key' => 'state_success', 'menu_name' => '已完成', 'menu_url' => url('seller/order/index?state_type=state_success')),
                    array('menu_key' => 'state_cancel', 'menu_name' => '已取消', 'menu_url' => url('seller/order/index?state_type=state_cancel')),
                );
                break;
        }
        $this->assign('member_menu', $menu_array);
        $this->assign('menu_key', $menu_key);
    }

}
